def write_preamble(n,note):
    """write nice preamble for latex file"""
    n.write(chr(92)+'documentclass[a4paper, final, 12pt]{article}\n')
    n.write(chr(92)+'usepackage{hyperref}\n')
    n.write(chr(92)+'usepackage{longtable}\n')
    n.write(chr(92)+'usepackage[utf8]{inputenc}\n')
    n.write(chr(92)+'usepackage{color}\n')
    n.write(chr(92)+'usepackage{lineno}\n')
    n.write(chr(92)+'usepackage{graphicx}\n')
    n.write(chr(92)+'renewcommand{'+chr(92)+'familydefault}{'+chr(92)+'sfdefault}\n')
    n.write(chr(92)+'usepackage{acronym}\n')
    n.write(chr(92)+'usepackage[left=2.75cm, right=2.75cm]{geometry}\n')
    n.write(chr(92)+'usepackage{soul}\n')
    n.write(chr(92)+'usepackage{mathrsfs}\n')
    n.write(chr(92)+'usepackage{epsfig}\n')
    n.write(chr(92)+'usepackage{mhchem}\n')
    n.write(chr(92)+'usepackage{amssymb}\n')
    n.write(chr(92)+'DeclareMathAlphabet{\mathpzc}{OT1}{pzc}{m}{it}\n')
    n.write(chr(92)+'def'+chr(92)+'bra#1{'+chr(92)+'mathinner{'+chr(92)+'langle{#1}|}}\n')
    n.write(chr(92)+'def'+chr(92)+'ket#1{'+chr(92)+'mathinner{|{#1}'+chr(92)+'rangle}}\n')
    n.write(chr(92)+'newcommand{'+chr(92)+'braket}[2]{'+chr(92)+'langle #1|#2'+chr(92)+'rangle}\n')
    n.write(chr(92)+'def'+chr(92)+'Bra#1{'+chr(92)+'left<#1'+chr(92)+'right|}\n')
    n.write(chr(92)+'def'+chr(92)+'Ket#1{'+chr(92)+'left|#1'+chr(92)+'right>}\n')
    n.write(chr(92)+'newcommand{'+chr(92)+'pdflaunch}[1] {'+chr(92)+'pdfpageattr{/AA << /O << /S /Launch /F (#1) >>>>}}\n')
    n.write(chr(92)+'newcommand{'+chr(92)+'pdflaunchlink}[2]{%\n')
    n.write('      '+chr(92)+'pdfannotlink attr{/Border [0 0 0]} user{/Subtype /Link /A << %\n')
    n.write('        /S /Launch /F (#1) >>}%\n')
    n.write('      '+chr(92)+'pdfliteral{0 1 0 0 k}%  \n')
    n.write('      {#2}'+chr(92)+'pdfliteral{0 0 0 1 k}'+chr(92)+'pdfendlink%\n')
    n.write('      }\n')
    n.write(''+chr(92)+'linespread{1.5}\n')
    n.write('%'+chr(92)+'graphicspath{pics/}\n')
    n.write(''+chr(92)+'newcommand{'+chr(92)+'instructions}[1]{{'+chr(92)+'color{blue}#1}}\n')
    n.write(''+chr(92)+'newcommand{'+chr(92)+'note}[1]{{'+chr(92)+'color{red}#1}}\n')
    n.write(''+chr(92)+'date{}\n')
    n.write(''+chr(92)+'begin{document}\n')
    n.write(''+chr(92)+'setcounter{secnumdepth}{3}\n')
    n.write(''+chr(92)+'setcounter{tocdepth}{2}\n')
    n.write(''+chr(92)+'setcounter{section}{0}\n')
    n.write(''+chr(92)+'begin{center}\n')
    n.write('{'+chr(92)+'huge{%s}\n' %note.split('/')[-1].replace('.note',''))
    n.write(''+chr(92)+'vspace{0.5cm}\n')
    n.write(''+chr(92)+'large\n')
    n.write('} '+2*chr(92)+'\n')
    n.write(''+chr(92)+'end{center}\n')
    n.write('%\maketitle\n')
    n.write(''+chr(92)+'vspace*{0.4cm}\n')
    n.write(''+chr(92)+'begin{center}\n')
    n.write(''+chr(92)+'large{F. Uhlig$^{'+chr(92)+'dagger}$~'+chr(92)+'footnote{email to: frank.uhlig@uochb.cas.cz}}\n')
    n.write(''+chr(92)+'vfill\n')
    n.write(''+chr(92)+'vspace{1cm}\n')
    n.write(''+chr(92)+'small\n')
    n.write('$^{'+chr(92)+'dagger}$ Physics 4F4 '+2*chr(92)+' Univerzita Karlova v Praze '+2*chr(92)+'\n')
    n.write('Matematicko-fyzik'+chr(92)+chr(39)+'{a}ln'+chr(92)+chr(39)+'{i} fakulta '+2*chr(92)+'[2ex]\n')
    n.write('%$^{'+chr(92)+'dagger}$ Institute of Organic Chemistry and Biochemistry AS CR, '+2*chr(92)+'\n')
    n.write('%Flemingovo n'+chr(92)+'{a}m. 2., '+2*chr(92)+' 166 10 Praha 6, '+2*chr(92)+' Czech Republic '+2*chr(92)+'[2ex]\n')
    n.write(''+chr(92)+'end{center}\n')
    n.write(''+chr(92)+'normalsize\n')
    n.write(''+chr(92)+'newpage\n')

def latexer(note):
    """convert simple plain text file into latex code"""

    ndxr = '    '

    f = open(note,'r')

    n = open(note.replace('.note','.tex'),'w')

    write_preamble(n,note)

    item = False
    nitem = 0
    sec = False

    for line in f.readlines():
        if line.startswith('pic'):
            n.write(chr(92)+'begin{figure}[hpbt]\n')
            n.write(chr(92)+'centering\n')
            n.write(chr(92)+'epsfig{file=%s ,width=0.5'%line.split()[1].replace('.eps','')+chr(92)+'linewidth,clip=}\n')
            n.write(chr(92)+'end{figure}\n')
            continue
            #\caption{Radial distribution functions of water oxygen atom (OW, in black) and methanol 
            #oxygen atom (O, in red) around oxygen atom of \ce{H2O} }
            #\label{fig:gofr_ow_o}
        for nsec in range(3):
            if line.startswith(nsec*'sub'+'sec'):
                n.write(chr(92)+nsec*'sub'+'section{')#+line.replace(nsec*'sub'+'sec','')+'}\n')
                seclines = line.split()[1:]
                for item in range(len(seclines)):
                        n.write(seclines[item])
                n.write('}\n')
                sec = True
        if sec:
            sec = False
            continue
        if not line.startswith(ndxr):
            if item and line != '':
                n.write(chr(92)+'end{itemize}\n')
                #n.write('hello\n')
                nitem -= 1
                if nitem == 0:
                    item = False
            n.write(line)
        elif line.startswith(ndxr):
            if item and not line.startswith(nitem*ndxr):
                n.write(chr(92)+'end{itemize}\n')
                #item = False
                nitem -= 1
            if not item and nitem == 0:
                n.write(chr(92)+'begin{itemize}\n')
                item = True
                nitem += 1
            if line.startswith((nitem+1)*ndxr):        #<--- something in this direction would be nice if it works
                n.write(chr(92)+'begin{itemize}\n')
                item = True
                nitem += 1
            n.write('   '+ chr(92)+'item %s \n' %line.strip())
        #n.write(str(nitem)+' '+str(item)+'\n')

    n.write(''+chr(92)+'end{document}\n')

    print 'written file %s' %note.replace('.note','.tex')
